package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import common.BaseFragment;
import common.Constant;
import https.InterNetController;
import in.srain.cube.views.ptr.PtrFrameLayout;
import mainFragment.activity.CollegeMoreDetail;
import topic.activity.DeliverBlog;
import topic.activity.TopicDetail;
import topic.adapter.TopicAdapter;
import topic.bean.TopicGroupBean;
import utils.MyLogUtils;
import view.MyPtrRefresh_LoadView;

/**
 * Created by Administrator on 2017/3/3.
 * 用于高校讨论小组
 */
@SuppressLint("ValidFragment")
public class TopicGroup extends BaseFragment {
    private View view;
    private Context context;
    private String id;
    private ListView college_topicgroup_lv;
    private TopicAdapter adapter;
    private ArrayList<TopicGroupBean.BlogListBean> arrayList;
    private Handler handler;
    private View headerView;
    private MyPtrRefresh_LoadView myRefresh_loadLayout;
    private ImageView college_topic_comment_ima;//评论按钮
    private final int ON_REFRESH = 1;
    private final int ON_LOAD = 2;
    private int page = 1;//当前页

    private static final String TAG = "TopicGroup";

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS:
                    Gson gson = new Gson();
                    TopicGroupBean topicGroupBean = gson.fromJson((String) msg.obj, TopicGroupBean.class);
                    if (topicGroupBean != null) {
                        List<TopicGroupBean.BlogListBean> blogList = topicGroupBean.getBlogList();
                        if (blogList != null) {
                            if (blogList.size() > 0) {
                                if (msg.arg1 == ON_REFRESH) {
                                    arrayList.clear();
                                }
                                for (int i = 0; i < blogList.size(); i++) {
                                    TopicGroupBean.BlogListBean bean = blogList.get(i);
                                    if (!arrayList.contains(bean)) {
                                        switch (msg.arg1) {
                                            case ON_REFRESH:
                                                arrayList.add(bean);
                                                myRefresh_loadLayout.OnRefreshComplete();
                                                break;
                                            case ON_LOAD:
                                                arrayList.add(bean);
                                                myRefresh_loadLayout.OnRefreshComplete();
                                                break;
                                            default:
                                                break;
                                        }
                                    }
                                }
                            } else {
                                if (msg.arg1 == ON_LOAD) {
                                    page--;
                                }
                                if (myRefresh_loadLayout != null) {
                                    myRefresh_loadLayout.OnRefreshComplete();
                                    break;
                                }
                            }
                        }
                        adapter.notifyDataSetChanged();
                    }
                    break;
                default:

                    break;
            }

        }
    };

    public TopicGroup(Context context) {
        this.context = context;
    }

    public TopicGroup(Context context, String id, Handler handler) {
        this.context = context;
        this.id = id;
        this.handler = handler;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_topicgroup, null, false);
        }
        if (getMyActivity() != null) {
            initView();
            showComment();
            Message message = new Message();
            message.what = CollegeMoreDetail.TOPICGROUPCOMPLETE;
            handler.sendMessage(message);
//            getDataFromInterNet(page = 1, ON_REFRESH);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return view;
    }

    private void initView() {
        headerView = LayoutInflater.from(context).inflate(R.layout.college_topic_header, null);
        myRefresh_loadLayout = (MyPtrRefresh_LoadView) view.findViewById(R.id.college_topic_refresh_load_layout);
        college_topic_comment_ima = (ImageView) view.findViewById(R.id.college_topic_comment_ima);
        myRefresh_loadLayout.setMyPtrHandler(new MyPtrRefresh_LoadView.MyPtrHandler() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                getDataFromInterNet(++page, ON_LOAD);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                getDataFromInterNet(page = 1, ON_REFRESH);
            }
        });
        arrayList = new ArrayList<>();

        adapter = new TopicAdapter(getMyActivity(), arrayList, new TopicAdapter.OnTopicDetailListener() {
            @Override
            public void onTopicDetailListener(int position) {
                Intent i = new Intent(getMyActivity(), TopicDetail.class);
                i.putExtra("blogid", arrayList.get(position).getBlogid());
                startActivityForResult(i, position);
            }
        });
        college_topicgroup_lv = (ListView) view.findViewById(R.id.college_topicgroup_lv);
        college_topicgroup_lv.addHeaderView(headerView);
        college_topicgroup_lv.setAdapter(adapter);
    }

    private void showComment() {
        //是否显示发表博客按钮
        if ("mainFragment.activity.CollegeIndex".equals(getMyActivity().getClass().getName())) {
            college_topic_comment_ima.setVisibility(View.GONE);
        } else {
            college_topic_comment_ima.setVisibility(View.VISIBLE);
            college_topic_comment_ima.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (getMyActivity().hasLoad()) {
                        Intent i = new Intent();
                        i.setClass(getMyActivity(), DeliverBlog.class);
                        i.putExtra("id", id);
                        startActivity(i);
                    } else {
                        getMyActivity().login(getMyActivity());
                    }
                }
            });
        }
    }

    public void setTopicGroup(TopicGroupBean topicGroup) {
        if (topicGroup == null) {
            page = 1;
            getDataFromInterNet(page = 1, ON_REFRESH);
        } else {
            page++;
            List<TopicGroupBean.BlogListBean> blogList = topicGroup.getBlogList();
            if (blogList != null) {
                if (blogList.size() > 0) {
                    arrayList.clear();
                    for (int i = 0; i < blogList.size(); i++) {
                        TopicGroupBean.BlogListBean bean = blogList.get(i);
                        if (!arrayList.contains(bean)) {
                            arrayList.add(bean);
                        }
                    }
                }
                if (adapter != null) {
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }

    private void getDataFromInterNet(int page, int flag) {
        if (id != null) {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("page", String.valueOf(page));
            hashMap.put("collegeid", id);
            new InterNetController(getMyActivity(), Constant.SHOWBLOG, myHandler, hashMap, flag, false);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        MyLogUtils.e(TAG, "resultCode" + resultCode);
        if (resultCode == 2) {
            int zan = data.getIntExtra("zan", 0);
            int comment = data.getIntExtra("comment", 0);
            boolean is_praised = data.getBooleanExtra("is_praised", false);
            boolean is_deleted = data.getBooleanExtra("is_deleted", false);
            if (is_deleted) {
                arrayList.remove(requestCode);
            } else {
                arrayList.get(requestCode).setComment(String.valueOf(comment));
                arrayList.get(requestCode).setPrise(String.valueOf(zan));
                if (is_praised) {
                    arrayList.get(requestCode).setPrised(1);
                } else {
                    arrayList.get(requestCode).setPrised(0);
                }
            }
            adapter.notifyDataSetChanged();
        }
    }
}
